package com.whisperarts.financer.app.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.whisperarts.financer.app.Application;
import com.whisperarts.financer.app.utils.Icons;

public class IncomeButton extends Composite {
	
	private boolean income = false;
	private final Button button;

	public IncomeButton(Composite parent, int style) {
		super(parent, style);
		
		FormToolkit toolkit = new FormToolkit(this.getDisplay());
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.makeColumnsEqualWidth = false;
		gridLayout.verticalSpacing = gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		button = toolkit.createButton(this, "", SWT.PUSH);
		button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, Icons.MINUS).createImage());
		button.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				setIncome(!isIncome());
			}				
		});
	}
	
	public boolean isIncome() {
		return income;
	}

	public void setIncome(boolean income) {
		this.income = income;
		updateIncomeImage();
	}

	private void updateIncomeImage() {
		if (isIncome()) {
			button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, Icons.PLUS).createImage());
		} else {
			button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, Icons.MINUS).createImage());
		}
	}
}
